<?php

namespace app\api\controller;


class Comment extends ApiBase {
    private $INFORMATION = 1;
    private $ARTICLE = 2;

    public function index() {
        $query = input('get.');
        $this->checkUserToken();
        if (!empty($query['user']) && !empty($query['type'])) {
            if ($query['type'] === 'comment') {
                $result = model('Comment')->getCommentList($this->user_id);
            }
            if ($query['type'] === 'reply') {
                $result = model('Reply')
                    ->with(['userInfo'])
                    ->where(['send_id' => $this->user_id])
                    ->order(['create_time' => 'desc'])
                    ->select();
            }
        }
        if (empty($result)) {
            return show(config('code.fail'), 'fail');
        }
        return show(config('code.success'), 'success', $result);
    }

    public function read($id) {
        $type = input('get.type');
        if (empty($type)) {
            return show(config('code.fail'), 1);
        }
        $condition['type'] = $type;
        $result            = model('Comment')->getCommentListById($id, $type);
        return show(config('code.success'), 0, $result);
    }

    public function save() {
        $params = input('param.');
        // 当parentId为空或者0的时候，为一级主评论，不为空或0时为二级回复评论
        if (empty($params['parentId'])) {
            $data = [
                'type'       => $params['type'],
                'message_id' => $params['messageId'],
                'user_id'    => $this->user_id,
                'content'    => $params['content'],
                'status'     => config('code.status_show')
            ];
            model('Comment')->add($data);
        } else {
            $data = [
                'type'       => $params['type'],
                'belong_id'  => $params['parentId'],
                'message_id' => $params['messageId'],
                'user_id'    => $this->user_id,
                'send_id'    => $params['sendId'],
                'content'    => $params['content'],
                'status'     => config('code.status_show')
            ];
            model('Reply')->add($data);
            model('Comment')->where(['id' => $params['parentId']])->setInc('replay_count');
        }

        if ($params['type'] == $this->ARTICLE) {
            model('Article')->where(['id' => $data['message_id']])->setInc('comment_count');
        } else {
            model('Information')->where(['id' => $data['message_id']])->setInc('comment_count');
        }

        //当评论类型为资讯时，并且为一级主评论，通知资讯发布者
        if ($params['type'] == $this->INFORMATION && empty($params['sendId'])) {
            $info = model('Information')->get($params['messageId']);
            model('User')->where(['id' => $info->user_id])->update(['hasUnRead' => 1]);
        }
        //当有人@时，通知被@用户
        if (!empty($params['sendId'])) {
            model('User')->where(['id' => $params['sendId']])->update(['hasUnRead' => 1]);
        }

        return show(config('code.success'), '评论成功');
    }

    public function update($id) {
        model('User')->where(['id' => $this->user_id])->update(['hasUnRead' => 0]);
        return show(config('code.success'), 'ok');
    }
}